from rest_framework import serializers
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from apps.backend import models

class PageNumberPagination(PageNumberPagination):
    page_size_query_param = 'size'
    page_size = 15
    max_page_size = 30
    def get_paginated_response(self, data):
        return {'total': self.page.paginator.count, 'list': data}

class MenuTreeModelSerializer(serializers.ModelSerializer):
    children = serializers.SerializerMethodField()
    class Meta:
        model = models.Menus
        fields = ["id", "pid", "icon", "menu_name", "path_name", "path", "sort", "is_active", "children"]   

    def get_children(self, obj):
        children = models.Menus.objects.filter(pid=obj.id).all()
        children = MenuTreeModelSerializer(instance=children, many=True).data
        return children

class MenuAllTreeModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Menus
        fields = ["id", "pid", "icon", "menu_name", "path_name", "path", "sort"]               

class RoleModelSerializer(serializers.ModelSerializer): 
    menus = MenuAllTreeModelSerializer(many=True)
    class Meta:
        model = models.Roles
        fields = ["id", "menus"]
 
class MenuModelSerializer(serializers.ModelSerializer):
    parent_name = serializers.SerializerMethodField()
    statu = serializers.SerializerMethodField()

    class Meta:
        model = models.Menus
        fields = ["id", "menu_name", "path_name", "path", "icon", "sort", "is_active", "pid", "parent_name", "statu"]
        extra_kwargs = {
            "pid": {"write_only": True},
            # "path_name": {"write_only": True},
            # "path": {"read_only": True},
            "icon": {"read_only": True}
        }
    
    def get_parent_name(self, obj):
        name = ''
        if obj.pid:
            parent = models.Menus.objects.get(pk=obj.pid)
            name =  parent.menu_name + '/' + obj.menu_name
            while parent.pid:
                parent = models.Menus.objects.get(pk=parent.pid)
                name = parent.menu_name + '/' + name
        else:
            name = "顶级"    
        return name
    
    def get_statu(self, obj):
        if obj.is_active:
            return "启用"
        else:
            return "禁用"

    def create(self, validated_data):
        return models.Menus.objects.create(**validated_data)
    
    def update(self, instance, validated_data):
        instance.menu_name = validated_data.get('menu_name', instance.menu_name)
        instance.path_name = validated_data.get('path_name', instance.path_name)
        instance.path = validated_data.get('path', instance.path)
        instance.icon = validated_data.get('icon', instance.icon)
        instance.sort = validated_data.get('sort', instance.sort)
        instance.is_active = validated_data.get('is_active', instance.is_active)
        instance.pid = validated_data.get('pid', instance.pid)
        instance.save()
        return instance

